package start.demo01.datatype.json.entity.editor.jsonEditor;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import start.demo01.datatype.json.interfaces.JSONEditor;

public class JSONGetEditor extends JSONEditor {

    private Object object;

    public Object getObject() {
        return object;
    }
    {
        name = "GET";
    }

    public JSONGetEditor(String args) {
        //mv,x 第一个参数指定存入变量名，第二个为取值
        super(args);
    }

    @Override
    protected void operationObject(JSONObject object, String key) {
        this.object = object.get(key);
    }

    @Override
    protected void operationArray(JSONArray array, int index) {
        this.object = array.get(index);
    }

    @Override
    protected void operation(JSONObject object) {
        String key;
        String var = null;
        String[] split = args.split(",");
        if (split.length > 1) {
            var = split[0];
            key = split[1];
        } else
            key = split[0];
        this.object = object.get(key);
        if (var != null)
            map.put(var, this.object);
        else
            map.put(key, this.object);
    }
}
